//
//  BSSRunningWaterWithdrawalDetailsViewController.m
//  PreschoolEducation
//
//  Created by 李传铎 on 2020/11/14.
//  Copyright © 2020 李传铎. All rights reserved.
//

#import "BSSRunningWaterWithdrawalDetailsViewController.h"
#import "BSSEnterpriseCertificationModel.h"

#import "BSSEnterpriseCertificationBillTextFieldCell.h"
#import "BSSEnterpriseCertificationBillAddPhotoCell.h"
#import "BSSEnterpriseCertificationTextFieldCell.h"

@interface BSSRunningWaterWithdrawalDetailsViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSArray                               *dataArr;
@property (nonatomic, strong) NSMutableArray                        *jsonArr;
@property (nonatomic, strong) UITableView                           *tableView;

@end

@implementation BSSRunningWaterWithdrawalDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.title = @"企业/公司认证";
    self.view.backgroundColor = colorCode(@"#F9F9F9");
    self.jsonArr = [NSMutableArray array];
    
    [self createUI];
    [self layoutUI];
    [self initParam];
}

- (void)initParam {
    //isEdit 1可以编辑 0不可以编辑
    
    self.dataArr = @[@[@{@"title":@"提现金额",@"placeholder":@"请上传商户logo",@"type":@"1",@"value":@"¥1000.00",@"isMandatory":@"1",@"key":@"licenseUrl",@"isEdit":@"0"},@{@"title":@"手续费",@"placeholder":@"EF英孚青少儿英语培训",@"type":@"1",@"value":@"¥20.00",@"isMandatory":@"1",@"key":@"storeName",@"isEdit":@"0"},@{@"title":@"提现到",@"placeholder":@"1234123",@"type":@"1",@"value":@"中国银行（尾号8888）",@"isMandatory":@"1",@"key":@"userName",@"isEdit":@"0"},@{@"title":@"流水号",@"placeholder":@"13000000000",@"type":@"6",@"value":@"12345677890",@"isMandatory":@"1",@"key":@"userIdno",@"isEdit":@"0"}]];
    
    for (int i = 0; i < self.dataArr.count; i ++) {
        NSMutableArray *arr = [NSMutableArray array];
        [arr addObjectsFromArray:[NSArray yy_modelArrayWithClass:[BSSEnterpriseCertificationModel class] json:self.dataArr[i]]];
        [self.jsonArr addObject:arr];
    }
    [_tableView reloadData];
}

- (void)createUI {
    self.title = @"详情";
    
//    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, __kScreenWidth__, __kSize(0))];
//    headerView.backgroundColor = __kColor(248, 248, 248);
//    self.tableView.tableHeaderView = headerView;
    
    [self.view addSubview:self.tableView];
  
}

- (void)layoutUI {
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(__kNavigationBarHeight__ + 15);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.bottom.mas_equalTo(0);
    }];
    
//    UIButton *submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    submitBtn.backgroundColor = colorCode(@"#FE8300");
//    [submitBtn setTitle:@"确认提交" forState:0];
//    submitBtn.titleLabel.font = fontCount(16);
//    [submitBtn setTitleColor:[UIColor whiteColor] forState:0];
//    [self.view addSubview:submitBtn];
//    submitBtn.layer.masksToBounds = YES;
//    submitBtn.layer.cornerRadius = 4;
//    [submitBtn addTarget:self action:@selector(submitClick) forControlEvents:UIControlEventTouchUpInside];
//    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(_tableView.mas_bottom).offset(20);
//        make.left.mas_equalTo(25);
//        make.right.mas_equalTo(-25);
//        make.height.mas_equalTo(40);
//    }];
}
//- (void)submitClick{
//
//    for (int i = 0; i < _jsonArr.count; i ++) {
//        BSSEnterpriseCertificationModel *model = _jsonArr[i];
//        if ([model.isMandatory isEqualToString:@"1"]) {
//            if (IsEmpty(model.value)) {
//                [MBProgressHUD showText:model.placeholder];
//                return;
//            }
//        }
//    }
//    NSString*deleUrls = @"";
//    NSMutableDictionary *jsonDict = [NSMutableDictionary dictionary];
//    for (BSSEnterpriseCertificationModel *model in _jsonArr) {
//        [jsonDict setValue:model.value forKey:model.key];
//        if ([model.type isEqualToString:@"4"]) {
//            if (!IsEmpty(model.deleUrls)) {
//                if (IsEmpty(deleUrls)) {
//                    deleUrls = model.deleUrls;
//                }else{
//                    deleUrls = [NSString stringWithFormat:@"%@%@",deleUrls,model.deleUrls];
//                }
//            }
//
//        }
//    }
//    [jsonDict setValue:_enterpriseStoreId forKey:@"storeId"];
//    [jsonDict setValue:deleUrls forKey:@"deleteUrls"];
//    NSString *urls = @"";
//    if (_isEnterprise) {
//        urls = @"";
//    }else{
//        urls = @"";
//    }
//    [MBProgressHUD showHUD];
//    _baseRequest = [[BSSRequest alloc] initWithURL:urls dic:jsonDict];
//    [_baseRequest startRequest:^(BSSRequestStatus requestStatus, NSString * _Nullable message, id  _Nullable responseObject) {
//        [MBProgressHUD hideHUD];
//        if (requestStatus == BSSRequestStatusSuccess) {
//
//        }else{
//            [MBProgressHUD showText:message];
//        }
//    }];
//}
#pragma mark - UITableViewDelegate, UITableViewDataSource

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 185;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *diView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 185)];
    diView.backgroundColor = colorCode(@"#ffffff");
    UIImageView *pointImageV1 = [[UIImageView alloc]init];
    [diView addSubview:pointImageV1];

    [pointImageV1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(24);
        make.left.mas_equalTo(20);
        make.width.mas_equalTo(16);
        make.height.mas_equalTo(16);
    }];
    
    UIView *lineView1 = [[UIView alloc]init];
    lineView1.backgroundColor = colorCode(@"#CCCCCC");
    [diView addSubview:lineView1];
    [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(pointImageV1.mas_centerX);
        make.top.mas_equalTo(pointImageV1.mas_bottom);
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(58);
    }];
    
    UIImageView *pointImageV2 = [[UIImageView alloc]init];
    [diView addSubview:pointImageV2];

    [pointImageV2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(lineView1.mas_bottom);
        make.left.mas_equalTo(20);
        make.width.mas_equalTo(16);
        make.height.mas_equalTo(16);
    }];
    UIView *lineView2 = [[UIView alloc]init];
    lineView2.backgroundColor = colorCode(@"#CCCCCC");
    [diView addSubview:lineView2];
    [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(pointImageV2.mas_centerX);
        make.top.mas_equalTo(pointImageV2.mas_bottom);
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(58);
    }];
    
    UIImageView *pointImageV3 = [[UIImageView alloc]init];
    [diView addSubview:pointImageV3];

    [pointImageV3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(lineView2.mas_bottom);
        make.left.mas_equalTo(20);
        make.width.mas_equalTo(16);
        make.height.mas_equalTo(16);
    }];
    UILabel *stateL1 = [[UILabel alloc]init];
    stateL1.text = @"发起提现申请";
    stateL1.font = [UIFont boldSystemFontOfSize:16];
    [diView addSubview:stateL1];
    
    [stateL1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(pointImageV1.mas_right).offset(6.5);
        make.centerY.mas_equalTo(pointImageV1.mas_centerY);
    }];
    
    UILabel *timeL1 = [[UILabel alloc]init];
    timeL1.text = @"2020.12.12 23:59:59";
    timeL1.font = fontCount(14);
    timeL1.textColor = colorCode(@"#999999");
    [diView addSubview:timeL1];
    [timeL1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(pointImageV1.mas_right).offset(6.5);
        make.top.mas_equalTo(stateL1.mas_bottom).offset(2);
    }];
    
    UILabel *stateL2 = [[UILabel alloc]init];
    stateL2.text = @"银行处理中";
    stateL2.font = [UIFont boldSystemFontOfSize:16];
    [diView addSubview:stateL2];
    
    [stateL2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(pointImageV1.mas_right).offset(6.5);
        make.centerY.mas_equalTo(pointImageV2.mas_centerY);
    }];
    
    UILabel *timeL2 = [[UILabel alloc]init];
    timeL2.text = @"预计3个工作日内到账";
    timeL2.font = fontCount(14);
    timeL2.textColor = colorCode(@"#999999");
    [diView addSubview:timeL2];
    [timeL2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(pointImageV1.mas_right).offset(6.5);
        make.top.mas_equalTo(stateL2.mas_bottom).offset(2);
    }];
    UILabel *stateL3 = [[UILabel alloc]init];
    stateL3.text = @"到账成功";
    stateL3.textColor  = colorCode(@"#999999");
    stateL3.font = [UIFont boldSystemFontOfSize:16];
    [diView addSubview:stateL3];
    
    [stateL3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(pointImageV1.mas_right).offset(6.5);
        make.centerY.mas_equalTo(pointImageV3.mas_centerY);
    }];
    
    NSString *state = @"0";
    if ([state isEqualToString:@"0"]) {
        stateL1.textColor = colorCode(@"#F34B4D");
        stateL2.textColor = colorCode(@"333333");
        pointImageV1.image = imageNameStr(@"redD");
        pointImageV2.image = imageNameStr(@"grayD");
        pointImageV3.image = imageNameStr(@"grayD");
    }else if ([state isEqualToString:@"1"]){
        
    }else if ([state isEqualToString:@"2"]){
        
    }
    return diView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.jsonArr[section] count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.jsonArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BSSEnterpriseCertificationModel *model = self.jsonArr[indexPath.section][indexPath.row];
    if ([model.type isEqualToString:@"1"]||[model.type isEqualToString:@"2"]||[model.type isEqualToString:@"3"]) {
        BSSEnterpriseCertificationTextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BSSEnterpriseCertificationTextFieldCell" forIndexPath:indexPath];
        
       
        NSString *title = model.title;
        NSString *placeholder = model.placeholder;
        
        NSRange range = [title rangeOfString:@"*"];
        NSMutableAttributedString *outStr = [[NSMutableAttributedString alloc] initWithString:title];
        [outStr addAttributes:@{NSForegroundColorAttributeName : __kColor(193, 49, 54)} range:range];
        cell.titleLab.attributedText = outStr;
        cell.textField.placeholder = placeholder;
        if ([model.isEdit isEqualToString:@"1"]) {
            cell.textField.userInteractionEnabled = YES;
        }else{
            cell.textField.userInteractionEnabled = NO;
        }
        if ([model.type isEqualToString:@"1"]) {
            cell.textField.keyboardType = UIKeyboardTypeDefault;
        }else{
            cell.textField.keyboardType = UIKeyboardTypeNumberPad;
        }
        cell.model = model;
        return cell;
    }else if ([model.type isEqualToString:@"4"]){
        
        BSSEnterpriseCertificationBillAddPhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BSSEnterpriseCertificationBillAddPhotoCell" forIndexPath:indexPath];
        
      
        NSString *title = model.title;
        NSRange range = [title rangeOfString:@"*"];
        NSMutableAttributedString *outStr = [[NSMutableAttributedString alloc] initWithString:title];
        [outStr addAttributes:@{NSForegroundColorAttributeName : __kColor(193, 49, 54)} range:range];
        cell.titleLab.attributedText = outStr;
        cell.model = model;
        if ([model.isEdit isEqualToString:@"1"]) {
            cell.licenseBtn.userInteractionEnabled = YES;
        }else{
            cell.licenseBtn.userInteractionEnabled = NO;
        }
        return cell;
        
    }else if ([model.type isEqualToString:@"7"]){
        
        BSSEnterpriseCertificationBillAddPhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BSSEnterpriseCertificationBillAddPhotoCell" forIndexPath:indexPath];
        
      
        NSString *title = model.title;
        NSRange range = [title rangeOfString:@"*"];
        NSMutableAttributedString *outStr = [[NSMutableAttributedString alloc] initWithString:title];
        [outStr addAttributes:@{NSForegroundColorAttributeName : __kColor(193, 49, 54)} range:range];
        cell.titleLab.attributedText = outStr;
        cell.model = model;
        if ([model.isEdit isEqualToString:@"1"]) {
            cell.licenseBtn.userInteractionEnabled = YES;
        }else{
            cell.licenseBtn.userInteractionEnabled = NO;
        }
        return cell;
        
    }else if ([model.type isEqualToString:@"5"]||[model.type isEqualToString:@"6"]){
        
        BSSEnterpriseCertificationBillTextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BSSEnterpriseCertificationBillTextFieldCell" forIndexPath:indexPath];
        
       
        NSString *title = model.title;
        NSRange range = [title rangeOfString:@"*"];
        NSMutableAttributedString *outStr = [[NSMutableAttributedString alloc] initWithString:title];
        [outStr addAttributes:@{NSForegroundColorAttributeName : __kColor(193, 49, 54)} range:range];
        cell.titleLab.attributedText = outStr;
        cell.textField.placeholder = model.placeholder;
        if ([model.isEdit isEqualToString:@"1"]) {
            cell.textField.userInteractionEnabled = YES;
        }else{
            cell.textField.userInteractionEnabled = NO;
        }
        cell.model = model;
        return cell;
        
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    BSSEnterpriseCertificationModel *model = self.jsonArr[indexPath.section][indexPath.row];
    
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    [self cornerRadiusCell:tableView willDisplayCell:cell forRowAtIndexPath:indexPath];
    
}



- (void)cornerRadiusCell:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    // 圆角弧度半径
    CGFloat cornerRadius = 5.f;
    
    // 设置cell的背景色为透明，如果不设置这个的话，则原来的背景色不会被覆盖
    cell.backgroundColor = UIColor.clearColor;
    
    // 创建一个shapeLayer
    CAShapeLayer *layer = [[CAShapeLayer alloc] init];
    CAShapeLayer *backgroundLayer = [[CAShapeLayer alloc] init]; //显示选中
    // 创建一个可变的图像Path句柄，该路径用于保存绘图信息
    CGMutablePathRef pathRef = CGPathCreateMutable();
    // 获取cell的size
    // 第一个参数,是整个 cell 的 bounds, 第二个参数是距左右两端的距离,第三个参数是距上下两端的距离
    CGRect bounds = CGRectInset(cell.bounds, 10, 0);
    
    // CGRectGetMinY：返回对象顶点坐标
    // CGRectGetMaxY：返回对象底点坐标
    // CGRectGetMinX：返回对象左边缘坐标
    // CGRectGetMaxX：返回对象右边缘坐标
    // CGRectGetMidX: 返回对象中心点的X坐标
    // CGRectGetMidY: 返回对象中心点的Y坐标
    
    // 这里要判断分组列表中的第一行，每组section的第一行，每组section的中间行
    
    // CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);

    if (indexPath.row == 0) {
        // 初始起点为cell的左下角坐标
        CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
        // 起始坐标为左下角，设为p，（CGRectGetMinX(bounds), CGRectGetMinY(bounds)）为左上角的点，设为p1(x1,y1)，(CGRectGetMidX(bounds), CGRectGetMinY(bounds))为顶部中点的点，设为p2(x2,y2)。然后连接p1和p2为一条直线l1，连接初始点p到p1成一条直线l，则在两条直线相交处绘制弧度为r的圆角。
        CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);
        CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
        // 终点坐标为右下角坐标点，把绘图信息都放到路径中去,根据这些路径就构成了一块区域了
        CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
        
    } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
        // 初始起点为cell的左上角坐标
        CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
        CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
        CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
        // 添加一条直线，终点坐标为右下角坐标点并放到路径中去
        CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
    } else {
        // 添加cell的rectangle信息到path中（不包括圆角）
        CGPathAddRect(pathRef, nil, bounds);
    }
    // 把已经绘制好的可变图像路径赋值给图层，然后图层根据这图像path进行图像渲染render
    layer.path = pathRef;
    backgroundLayer.path = pathRef;
    // 注意：但凡通过Quartz2D中带有creat/copy/retain方法创建出来的值都必须要释放
    CFRelease(pathRef);
    // 按照shape layer的path填充颜色，类似于渲染render
    // layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.8f].CGColor;
    layer.fillColor = [UIColor whiteColor].CGColor;
    
    // view大小与cell一致
    UIView *roundView = [[UIView alloc] initWithFrame:bounds];
    // 添加自定义圆角后的图层到roundView中
    [roundView.layer insertSublayer:layer atIndex:0];
    roundView.backgroundColor = UIColor.clearColor;
    // cell的背景view
    cell.backgroundView = roundView;
    
}

#pragma mark - setter & getter

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = __kColor(248, 248, 248);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _tableView.estimatedRowHeight = 200;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[BSSEnterpriseCertificationTextFieldCell class] forCellReuseIdentifier:@"BSSEnterpriseCertificationTextFieldCell"];
        [_tableView registerClass:[BSSEnterpriseCertificationBillAddPhotoCell class] forCellReuseIdentifier:@"BSSEnterpriseCertificationBillAddPhotoCell"];
        [_tableView registerClass:[BSSEnterpriseCertificationBillTextFieldCell class] forCellReuseIdentifier:@"BSSEnterpriseCertificationBillTextFieldCell"];
    }
    
    return _tableView;
}

@end
